from django.shortcuts import render
from django.http import HttpResponse
from django.conf import settings
from django.core.cache import caches,cache
import csv
import time
from fundinfo.fundmodel import *
from analysis.basic_data import *
from django import template

register = template.Library()

def getMaxDrawDownYearFun(code):
    return BasicData.getMaxDrawDownYear(code)

class FundData:
    tradingData = None
    maxDrawDownYear = 0.0
    incomeYear = 0.0
    civilYearRate = []

class FundAnalysisData:
    fundName = None
    fundInfo = None

def getFundList(request):
    info = {}
    lst = []

    fundLst = BasicData.getTradingFundCodeLst()
    for code in fundLst:
        infoBrief = FundInfoBriefData()
        data = FundAnalysisData()
        data.fundName = infoBrief.getFundName(code)
        data.fundInfo = infoBrief.getExsitRecord(code)
        lst.append(data)

    info['funds'] = sorted(lst, key=lambda fund: fund.fundInfo.maxDrawDown1Y)
    return render(request,"fundlist.html", info)    


def getCategoryList(request):
    info = {}
    info['categorys'] = CustomCategoryTools.getCategorysIncomeData()
    return render(request,"category.html", info)

def getFundListByCategorySortName(request, category, sortName):
    info = {}

    funds = CustomCategoryTools.getFundsByCategory(category)

    lst = []

    for fund in funds:
        code = fund.code
        infoBrief = FundInfoBriefData()
        data = FundAnalysisData()
        data.fundName = infoBrief.getFundName(code)
        data.fundInfo = infoBrief.getExsitRecord(code)
        if data.fundInfo == None or data.fundName == None:
            continue
        if data.fundInfo.curValue == 0.0:
            continue
        lst.append(data)
    
    if sortName == "curIncomeRate":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.curIncomeRate, reverse=True)
    elif sortName == "curValue":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.curValue, reverse=True)
    elif sortName == "totalIncome":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.totalIncome, reverse=True)
    elif sortName == "curIncome":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.curIncome, reverse=True)
    elif sortName == "maxDrawDownRate":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.icomeDrawDownRate1Y, reverse=True)
    elif sortName == "maxDrawDown2Y":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.maxDrawDown2Y)
    elif sortName == "maxDrawDown3Y":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.maxDrawDown3Y)
    elif sortName == "maxDrawDown5Y":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.maxDrawDown5Y)
    elif sortName == "6MRate":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.incomeRate6M, reverse=True)
    elif sortName == "1YRate":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.incomeRate1Y, reverse=True)
    elif sortName == "2YRate":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.incomeRate2Y, reverse=True)
    elif sortName == "3YRate":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.incomeRate3Y, reverse=True)
    elif sortName == "5YRate":
        lst = sorted(lst, key=lambda fund: fund.fundInfo.incomeRate5Y, reverse=True)
    else:
        lst = sorted(lst, key=lambda fund: fund.fundInfo.maxDrawDown1Y)

    info['funds'] = lst

    info['categorys'] = CustomCategoryTools.getCategorysIncomeData()

    if category != None or category != "":
        info['categoryFilter'] = True
    else:
        info['categoryFilter'] = False
    info['categoryFilter'] = category

    return render(request,"category.html", info)

def getFundListByCategory(request, category):
    return getFundListByCategorySortName(request, category, "")
